import java.util.List;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class CampusCardSystem {
    private static List<User> users = new ArrayList<>();
    private static Admin admin = new Admin(users);
    private static Scanner scanner = new Scanner(System.in);



    public static void main(String[] args) {
        users = User.loadUserData(); // 通过实例调用,初始化用户
        mainMenu();
    }

    private static void mainMenu() {
        while (true) {
            clearConsole();
            //printAllUsers();
            System.out.println("/——————————————————\\");
            System.out.println("     1.用户登录     ");
            System.out.println("     2.管理员登录   ");
            System.out.println("     3.退出        ");
            System.out.println("\\——————————————————/");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 处理换行

            switch (choice) {
                case 1:
                    userLogin();
                    break;
                case 2:
                    adminLogin();
                    break;
                case 3:
                    System.exit(0);
                default:
                    System.out.println("无效选项，请重试。");
            }
        }
    }

    private static void printAllUsers() {
        if (users.isEmpty()) {
            System.out.println("用户列表为空");
        } else {
            for (User user : users) {
                System.out.println(user);
            }
        }
    }
    private static void userLogin() {
        System.out.print("输入学号：");
        String studentId = scanner.nextLine();
        User user = findUserById(studentId);



        if (user == null) {
            System.out.println("用户不存在！");
            return;
        }
// 显示用户姓名
        System.out.println("欢迎，" + user.getName() + "！");

        // 输入密码
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        // 验证密码
        if (!user.verifyPassword(password)) {
            System.out.println("密码错误！");
            return;
        }
        userMenu(user);
    }

    private static void userMenu(User user) {
        clearConsole();
        while (true) {
            System.out.println("/——————————————————\\");
            System.out.println("  请输入要执行的操作");
            System.out.println("     1. 修改密码");
            System.out.println("     2. 充值");
            System.out.println("     3. 消费");
            System.out.println("     4. 查询消费记录");
            System.out.println("     5. 挂失");
            System.out.println("     6. 解挂");
            System.out.println("     7. 退出登录");
            System.out.println("\\——————————————————/");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.print("请输入旧密码：");
                    String password = scanner.nextLine();

                    // 验证密码
                    if (!user.verifyPassword(password)) {
                        System.out.println("密码错误！");
                        break;
                    }
                    System.out.print("输入新密码：");
                    String newPassword = scanner.nextLine();
                    user.modifyPassword(newPassword);

                    break;
                case 2:
                    System.out.print("输入充值金额：");
                    double amount = scanner.nextDouble();
                    user.recharge(amount);
                    break;
                case 3:
                    System.out.print("输入消费金额：");
                    double cost = scanner.nextDouble();
                    System.out.print("输入消费地点：");
                    String location = scanner.next();
                    user.consume(cost, location);
                    break;
                case 4:
                    System.out.print("输入起始日期（yyyy-MM-dd）：");
                    Date startDate = parseDate(scanner.nextLine());
                    System.out.print("输入结束日期（yyyy-MM-dd）：");
                    Date endDate = parseDate(scanner.nextLine());
                    System.out.print("输入消费地点：");
                    String locationQuery = scanner.nextLine();
                    user.showTransactions(startDate, endDate, locationQuery);
                    break;
                case 5:
                    user.requestLoss();
                    break;
                case 6:
                    user.requestRecovery();
                    break;
                case 7:
                    return;
                default:
                    System.out.println("无效选项，请重试。");
            }
            user.showBalance(); // 每次操作后显示余额提醒
        }
    }


//确保输入的数字为正数
    private static int getIntInput() {
        while (true) {
            try {
                return Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.print("请输入一个有效的数字：");
            }
        }
    }
    private static double getPositiveDoubleInput() {
        while (true) {
            try {
                double value = Double.parseDouble(scanner.nextLine());
                if (value > 0) {
                    return value;
                } else {
                    System.out.print("请输入一个正数：");
                }
            } catch (NumberFormatException e) {
                System.out.print("请输入一个有效的数字：");
            }
        }
    }


    private static void adminLogin() {
        System.out.print("输入管理员用户名：");
        String username = scanner.nextLine();
        System.out.print("输入管理员密码：");
        String password = scanner.nextLine();

        if (admin.login(username, password)) {
            adminMenu();
        } else {
            System.out.println("用户名或密码错误！");
        }
    }

    private static void adminMenu() {
        clearConsole();  // 清屏
        while (true) {
            System.out.println("/————————————————————\\");
            System.out.println("     1. 查看所有的用户");
            System.out.println("     2. 确认挂失");
            System.out.println("     3. 确认解挂");
            System.out.println("     4. 查看消费统计");
            System.out.println("     5. 退出登录");
            System.out.println("\\————————————————————/");
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:printAllUsers();
//                    System.out.print("输入用户学号：");
//                    String studentId = scanner.nextLine();
//                    User user = findUserById(studentId);
//                    if (user != null) {
//                        admin.approveUser(user);
//                    } else {
//                        System.out.println("用户不存在！");
//                    }
                    break;
                case 2:
                    System.out.print("输入用户学号：");
                    String lostId = scanner.nextLine();
                    User lostUser = findUserById(lostId);
                    if (lostUser != null) {
                        lostUser.confirmLoss(); // 确认挂失请求
                    } else {
                        System.out.println("用户不存在！");
                    }
                    break;
                case 3:
                    System.out.print("输入用户学号：");
                    String recoverId = scanner.nextLine();
                    User recoverUser = findUserById(recoverId);
                    if (recoverUser != null) {
                        recoverUser.confirmRecovery(); // 确认解挂请求
                    } else {
                        System.out.println("用户不存在！");
                    }
                    break;
                case 4:
                    System.out.print("输入起始日期（yyyy-MM-dd）：");
                    Date startDate = parseDate(scanner.nextLine());
                    System.out.print("输入结束日期（yyyy-MM-dd）：");
                    Date endDate = parseDate(scanner.nextLine());
                    System.out.print("输入消费地点：");
                    String location = scanner.nextLine();
                    admin.showStatistics(users, startDate, endDate, location);
                    break;
                case 5:
                    return;
                default:
                    System.out.println("无效选项，请重试。");
            }
        }
    }

    private static User findUserById(String studentId) {
        for (User user : users) {
            if (user.studentId.equals(studentId)) {
                return user;
            }
        }
        return null;
    }

    private static Date parseDate(String dateString) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
        } catch (ParseException e) {
            System.out.println("日期格式错误，请使用yyyy-MM-dd格式。");
            return new Date(); // 返回当前日期
        }
    }

    public static void clearConsole() {
        for (int i = 0; i < 50; i++) {
            System.out.println();
        }
    }

}